/*
 * @Author: chenxuan
 * @Date: 2021-09-06 09:01:22
 * @LastEditTime: 2021-10-15 13:40:14
 * @LastEditors: chenxuan
 */
import http from '@/axiosConfig/http'
import router from "@/router";
import store from '@/store'
import i18n from '@/i18n/i18n'
import {msg_info} from '@/assets/js/common.js'
// const auth=process.env.VUE_APP_BASE_URL_AUTH;
// const Vauth=process.env.VUE_APP_VERSION_AUTH;
let authRoute=[],arrs = [];
getMenus();
function getMenus(){
    http.post_('/login/getMenuResources')
    .then((res)=>{
        router.addRoutes(addRoute( filterMenus(originMenus(res.data.data))))
    })
    .catch((err)=>{})
}
function originMenus(arr){
    arr.forEach((item)=>{
        if(item.childrens){
            originMenus(item.childrens)
        }
        else{
            arrs.push(item)
        }
    })
    return arrs
}
function filterMenus(arr){
    return arr.filter((item)=>{
        return item.menuUrl.indexOf('unitizationApp')>0 && item.menuType=='1'
    })
}
function addRoute(arr){
    if(!window.__POWERED_BY_QIANKUN__){
        getChoasMenus(arr)
    }
    for(let i = 0; i < arr.length; i++){
        let obj = {};
        obj.path = `${arr[i].menuUrl.substring(arr[i].menuUrl.lastIndexOf('/'))}`;
        obj.name = arr[i].menuName;
        obj.component=(resolve,reject)=>import(/* webpackChunkName: `chaosApp` */ `@/${arr[i].menuLocation}`)
		.catch((err)=>{
        msg_info("【"+err+"】"+i18n.t('layout.comeBacktohome'),'error',6000,true);
			 setTimeout(()=>{
				 store.state.baseRouter.push('/index')
			   },6000);  
		})
        authRoute.push(obj);
    }
    return authRoute;
}
function getChoasMenus(originMneus){
    let ChaosMenus=[];
    for(let i = 0; i < originMneus.length; i++){
        let obj={};
        obj.menuUrl =`${originMneus[i].menuUrl.substring(originMneus[i].menuUrl.lastIndexOf('/'))}`;
        obj.id=originMneus[i].id;
        obj.menuName=originMneus[i].menuName;
        obj.menuCode=originMneus[i].menuCode;
        obj.icon=originMneus[i].icon;
        ChaosMenus.push(obj);
    }
    store.commit('loadChaosMenus',ChaosMenus);
}